import 'dart:developer' as developer;

class LColorPrintColor {
  /// 黑色
  static const String colorBlack = '\x1B[30m';

  /// 红色
  static const String colorRed = '\x1B[31m';

  /// 绿色
  static const String colorGreen = '\x1B[32m';

  /// 黄色
  static const String colorYellow = '\x1B[33m';

  /// 蓝色
  static const String colorBlue = '\x1B[34m';

  /// 玫红色
  static const String colorMagenta = '\x1B[35m';

  /// 青色
  static const String colorCyan = '\x1B[36m';

  /// 白色
  static const String colorWhite = '\x1B[37m';
}

class LColorPrint {
  static const String _colorClose = '\x1B[0m';
  static bool _isDebug = true;
  static bool _parseJson = false;
  static String _defaultColor = LColorPrintColor.colorWhite;
  static bool _isAndroid = true;

  static _unPrint() {
    return !_isDebug;
  }

  /// 配置日志打印选项 \
  /// @param isDebug 是否打印 \
  /// @param defaultColor 默认颜色 \
  /// @param parseJson 是否解析json 不完整的json也能按格式输出 \
  /// @param isAndroid 是否是android
  static init(
      {bool? isDebug, String? defaultColor, bool? parseJson, bool? isAndroid}) {
    _isDebug = isDebug == true;
    _defaultColor = defaultColor ?? _defaultColor;
    _parseJson = parseJson == true;
    _isAndroid = isAndroid == true;
  }

  /// 打印数组内容 拼成一个串，每一条内容按颜色下标显示对应颜色，颜色数量小于输出数量时用最后一个颜色
  static colour(List infos, List<String> colors) {
    if (_unPrint()) return;
    var str = '';
    for (var i = 0; i < infos.length; i++) {
      var tmp = infos[i].toString();
      var color = '';
      if (colors.length > i) {
        color = colors[i];
      } else {
        color = colors.isNotEmpty ? colors.last : _defaultColor;
      }
      if (_parseJson) {
        tmp = jsonParse(tmp);
      }
      if (tmp.contains('\n')) {
        tmp = tmp.split('\n').join('$_colorClose\n$color');
      }
      str += color + tmp + _colorClose;
    }
    if (!_isAndroid) {
      str = str.split('\n').map((e) {
        var s = e.replaceAll(RegExp(r'\x1B\[[0-9]+m'), '');
        return s;
      }).join('\n');
    }
    developer.log(str, name: '💚');
  }

  /// 把字符串转成带换行、空格错位的字符串
  static jsonParse(String s) {
    var tmp = s;
    if (tmp.contains('{}')) {
      tmp = tmp.split('{}').join('~');
    }
    if (tmp.contains('[]')) {
      tmp = tmp.split('[]').join('·');
    }
    if (tmp.contains('{')) {
      tmp = tmp.split('{').join('{\n');
    }
    if (tmp.contains('}')) {
      tmp = tmp.split('}').join('\n}');
    }
    if (tmp.contains('[')) {
      tmp = tmp.split('[').join('[\n');
    }
    if (tmp.contains(']')) {
      tmp = tmp.split(']').join('\n]');
    }
    if (tmp.contains(',')) {
      tmp = tmp.split(',').join(',\n');
    }
    if (tmp.contains('~')) {
      tmp = tmp.split('~').join('{}');
    }
    if (tmp.contains('·')) {
      tmp = tmp.split('·').join('[]');
    }
    var space = 0;
    var list = tmp.split('\n');
    var tlist = [];
    var pre = '';
    for (var s in list) {
      if (s.isNotEmpty != true) {
        continue;
      }
      if (s.trim().startsWith(RegExp(r'^[\[\{]'))) {
        if (pre == '{') space += 1;
        if (pre == '}') space += 0;
        if (pre == '') space += 0;
        pre = '{';
        s = '$_colorClose${LColorPrintColor.colorCyan}$s$_colorClose';
      } else if ((s != '{' && s.trim().endsWith('{')) ||
          (s != '[' && s.trim().endsWith('['))) {
        if (pre == '{') space += 1;
        if (pre == '}') space += 0;
        if (pre == '') space += 0;
        pre = '{';
      } else if ((s != '},' && s.trim().endsWith('},')) ||
          (s != '],' && s.trim().endsWith('],'))) {
        if (pre == '{') space += 1;
        if (pre == '}') space += 0;
        if (pre == '') space += 0;
        pre = '';
      } else if (s.trim().startsWith(RegExp(r'^[\]\}]'))) {
        if (pre == '{') space += 0;
        if (pre == '}') space -= 1;
        if (pre == '') space -= 1;
        pre = '}';
        s = '$_colorClose${LColorPrintColor.colorCyan}$s$_colorClose';
      } else {
        if (pre == '{') space += 1;
        if (pre == '}') space -= 0;
        if (pre == '') space -= 0;
        pre = '';
      }
      var tspace = '';
      for (var i = 0; i < space; i++) {
        tspace += '  ';
      }
      s = tspace + s.trim();
      if (s.trim().isNotEmpty == true) {
        if (s.contains(':')) {
          s = s.split(':').join(':$_colorClose${LColorPrintColor.colorCyan}');
        }
        tlist.add(s);
      }
    }
    tmp = tlist.join('\n');
    return tmp;
  }

  /// 按默认颜色打印 默认颜色可通过`LColorPrint.init()`设置
  LColorPrint(s) {
    colour([s.toString()], [_defaultColor]);
  }

  /// 打印红色信息 一般用于错误
  static error(s) {
    colour([s.toString()], [LColorPrintColor.colorRed]);
  }

  /// 打印黄色信息
  static warning(s) {
    colour([s.toString()], [LColorPrintColor.colorYellow]);
  }

  /// 打印青色信息 一般用于警告
  static info(s) {
    colour([s.toString()], [LColorPrintColor.colorCyan]);
  }

  /// 打印绿色信息 一般用于成功
  static success(s) {
    colour([s.toString()], [LColorPrintColor.colorGreen]);
  }

  /// 打印紫色信息
  static magenta(s) {
    colour([s.toString()], [LColorPrintColor.colorMagenta]);
  }

  /// 打印黑色信息
  static black(s) {
    colour([s.toString()], [LColorPrintColor.colorBlack]);
  }

  /// 打印蓝色信息
  static blue(s) {
    colour([s.toString()], [LColorPrintColor.colorBlue]);
  }

  /// 打印白色信息
  static white(s) {
    colour([s.toString()], [LColorPrintColor.colorWhite]);
  }
}
